﻿@{
    Uri url = new Uri(Request.Url.AbsoluteUri);
    ViewBag.Server = url.GetLeftPart(UriPartial.Authority);
    string crudServiceBaseUrl = ViewBag.Server + "/api/";
    string RafikiMasterVM = System.Configuration.ConfigurationManager.AppSettings["RafikiMasterVM"];
    ViewBag.td = "CURRENT.Term";
    string SearchList = System.Configuration.ConfigurationManager.AppSettings["DefaultSearch"];
}
<style>
    .inputbox {
        width: 100%;
    }
</style>

<script src="~/Scripts/kendo/2014.3.1119/kendo.ui.core.min.js"></script>
  <div id="search">
    <h2>Master Search (Students, Staff, Courses, Dorms, Email etc.)</h2>
    <p>Type part of a name of a course or a student or a staff and then SELECT to see more information.</p>
    <p>Search is CASE SENSITIVE. Type <b>Da</b> instead of <b>da</b> to see <b>Da</b>vid or <b>Da</b>ubenmier rather than Jor<b>da</b>n.</p>
      <input id="searchbox" data-role="autocomplete"
             data-text-field="LOOKUPNAME" data-filter="contains"
             data-bind="source: lookupSource, value: selectedLookupObject, events: {change: onLookupChange, filtering: screenfilter}"
             data-template="searchTemplate"
             data-min-length="3"
             data-highlight-first="true"
             class="inputbox"
             data-ignore-case="false"
             data-delay="500"
             onclick="select();" />
</div>
<div id="ajaxLookupContent"></div>

<script type="text/x-kendo-template" id="searchTemplate">
    <span class="#:ENTITYTYPE#"><img src="images/student.png" /> #:LOOKUPNAME#</span>
</script>

<script>
    var searchTemplate = kendo.template($("#searchTemplate").html);

    $(document).ready(function () {

        SearchViewModel = kendo.observable({

            selectedLookupObject: null,
            lastfilter: "",

            lookupSource: new kendo.data.DataSource({

                serverFiltering: true,
                batch: true,
                transport: {
                    read: {
                        url: '@crudServiceBaseUrl' + 'readnew/@(SearchList)/LookupList?TD=CURRENT.Term&JSONPARAMETERS={"@@SchoolDIVID":"@ViewBag.SchoolDIVID"}',
                        dataType: "json",
                        sort: { field: 'LOOKUPNAME', dir: 'asc' }
                    },
                    parameterMap: function (data, type) {
                        if (type == "read") {
                            return {
                                filter: data.filter.filters[0].value
                            }
                        }
                    }
                }
            }),

            screenfilter: function (e) {
                var filter = e.filter;
               if (!filter.value) {
                    //prevent filtering if the filter does not value
                    e.preventDefault();
                }
            },

            onLookupChange: function () {
                alert(this.selectedLookupObject["ENTITYID"]);
                var loadinfo = "HOME/SHOWENTITY" +"?entityid=" + this.selectedLookupObject["ENTITYID"].toString() +
                                            "&ChainSoFar=;" + 
                                            "&EntityType=" + this.selectedLookupObject["ENTITYTYPE"].toString() +
                                            "&TD=CURRENT.Term";
                    $("#ajaxLookupContent").load(loadinfo);
            }

        });

        kendo.bind($("#search"), SearchViewModel);
        $("#searchbox").value = "";
        $("#searchbox").focus();
    });
</script>




